package com.ysyc.driver_ee.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.tools.PermissionUtils;
import com.ysyc.driver_ee.widget.SureAndCancelDialog;
import com.ysyc.driver_ee.widget.TitleView;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/3 16:40<br>
 * 版本： v1.0<br>
 */
public class ContactCaptainActivity extends BaseActivity {
    @BindView(R.id.title_contact_captain_view)
    TitleView titleContactCaptainView;
    @BindView(R.id.tv_car_number_view)
    TextView tvCarNumberView;
    @BindView(R.id.tv_captain_name_view)
    TextView tvCaptainNameView;
    @BindView(R.id.tv_captain_phone_view)
    TextView tvCaptainPhoneView;


    private SureAndCancelDialog sureAndCancelDialog;
    private DriverInfo driverInfos;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_contact_captain;
    }


    @Override
    protected void initView() {
        initNetworkCallback();
        titleContactCaptainView.setLeftImg(R.drawable.left_return_icon);
        titleContactCaptainView.setLeftImgClick(v -> finish());
        titleContactCaptainView.setLeftText("联系队长");
        titleContactCaptainView.setBgDra(R.drawable.greed_02_1f_rounded_ractangle);
    }

    private void initNetworkCallback() {
        NetworkCallback<DriverInfo> tNetworkCallback = new NetworkCallback<>(new HttpResource<DriverInfo>() {
            @Override
            public void onSuccess(DriverInfo driverInfo) {
                dismissDilog();
                driverInfos = driverInfo;
                tvCarNumberView.setText(driverInfo.carFormCn + driverInfo.carNumber);
                tvCaptainNameView.setText(driverInfo.driverName);
                tvCaptainPhoneView.setText(driverInfo.telPhone);
            }

            @Override
            public void onFailure(String errorDes) {
                dismissDilog();
            }

            @Override
            public void onLoading(Dialog dialog) {
                showLoadingDialog();
            }
        });
        Observable<DriverInfo> driverInfoObservable = RetrofitClient.getInstance().getHttpServices().obtainCaptainInfo();
        tNetworkCallback.postRequest(driverInfoObservable, null);
    }

    public void callCaptain(View view) {
        String[] permisson = {Manifest.permission.CALL_PHONE};
        PermissionUtils.requestPermissions(ContactCaptainActivity.this, 1, permisson, mPermissionGrant);
    }

    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case 1:
                    if (sureAndCancelDialog == null) {
                        sureAndCancelDialog = new SureAndCancelDialog(ContactCaptainActivity.this);
                    }
                    sureAndCancelDialog.setTitleText("拨打队长电话").setMessageText(driverInfos.telPhone).setSureText("拨打电话").setOnDialogClickListener(new SureAndCancelDialog.onDialogClickListener() {
                        @Override
                        public void onPositiveButtonClick(SureAndCancelDialog dialog) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_CALL);
                            intent.setData(Uri.parse("tel:" + driverInfos.telPhone));
                            //开启系统拨号器
                            startActivity(intent);
                        }
                    });
                    sureAndCancelDialog.show();
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }


}
